﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Linq;

namespace SlamCms.SharePoint.WebParts
{
	[ToolboxItemAttribute(false)]
	public class ExternalPage : WebPart
	{	
		private string _url = "";
		[Personalizable(PersonalizationScope.Shared),
		 WebBrowsable(true),
		 Category("Settings"),
		 WebDisplayName("URL to load")]
		public string Url
		{
			get
			{
				return _url;
			}
			set
			{
				_url = value;
			}
		}

		private int _height = 900;
		[Personalizable(PersonalizationScope.Shared),
		 WebBrowsable(true),
		 Category("Settings"),
		 WebDisplayName("Height of IFrame in Pixels")]
		public int FrameHeight
		{
			get
			{
				return _height;
			}
			set
			{
				_height = value;
			}
		}

		protected override void CreateChildControls()   
		{
			HtmlGenericControl iFrame = new HtmlGenericControl("iframe");
			iFrame.ID = "externalPageFrame";
			iFrame.Attributes.Add("width", "100%");
			iFrame.Attributes.Add("height", FrameHeight.ToString());
			iFrame.Attributes.Add("frameborder", "0");
			iFrame.Attributes.Add("scrolling", "yes");
			iFrame.Attributes.Add("marginwidth", "0");
			iFrame.Attributes.Add("marginheight", "0");
			string source = Url;
			if (!String.IsNullOrEmpty(Convert.ToString(Page.Request.QueryString)))
			{
				source += (source.Contains("?") ? "&" : "?") + Page.Request.QueryString.ToString().TrimStart('?') + "&";
			}
			else
			{
				source += "?";
			}
			source += "TopSource=" + HttpUtility.UrlEncode(Page.Request.Url.ToString());
			iFrame.Attributes.Add("src", source);
			Controls.Add(iFrame);
		}
	}
}
